package com.global.domain;

import java.util.Collection;
import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.global.emc.core.CoreUser;
import com.global.emc.spring.method.support.chain.command.token.ResubmitToken;
import com.global.emc.spring.security.chain.command.password.UserPassword;

public class User implements UserDetails, CoreUser, UserPassword, ResubmitToken {

	private static final long serialVersionUID = -8816085301727960067L;

	private String uniqueId;
	private int id;
	@Length(min=1,max=40,message="username length error")
	private String username;
	private String password;
	private String tokenUniqueId;
	private boolean accountNonExpired;
	private boolean accountNonLocked;
	private boolean credentialsNonExpired;
	private boolean enabled;
	
	private Cif cif;
	private Collection<? extends GrantedAuthority> authorities;
	private List<Role> roles;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	@Override
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String getTokenUniqueId() {
		return tokenUniqueId;
	}
	public void setTokenUniqueId(String tokenUniqueId) {
		this.tokenUniqueId = tokenUniqueId;
	}
	@Override
	public String getUniqueId() {
		return uniqueId;
	}
	public void setUniqueId(String uniqueId) {
		this.uniqueId = uniqueId;
	}
	@Override
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}
	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}
	@Override
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}
	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}
	@Override
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}
	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}
	@Override
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}
	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	public Cif getCif() {
		return cif;
	}
	public void setCif(Cif cif) {
		this.cif = cif;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	public static User convertCoreUser(CoreUser coreUser){
		return (User)coreUser;
	}
	
	@Override
	public String toString() {
		return "User [uniqueId=" + uniqueId + ", id=" + id + ", username="
				+ username + ", password=" + password + ", tokenUniqueId="
				+ tokenUniqueId + ", accountNonExpired=" + accountNonExpired
				+ ", accountNonLocked=" + accountNonLocked
				+ ", credentialsNonExpired=" + credentialsNonExpired
				+ ", enabled=" + enabled + ", authorities=" + authorities + "]";
	}
	
}
